//
//  EditAddressViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/22.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "EditAddressViewController.h"
#import "AddAddressStore.h"
#import "AddressModel.h"
#import "ZHFAddTitleAddressView.h"
#import "AddressList.h"
#import "AddAddressParametersModel.h"

@interface EditAddressViewController ()<ZHFAddTitleAddressViewDelegate>
{
    
    __weak IBOutlet UITextField *peopleTf;
    __weak IBOutlet UITextField *phoneTf;
    __weak IBOutlet UITextField *localTf;
    __weak IBOutlet UITextField *detalLocalTf;
    
    AddAddressParametersModel *parametersModel;
}

@property(nonatomic,strong)ZHFAddTitleAddressView * addTitleAddressView;

@end

@implementation EditAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"编辑地址"];
    
    
    [self setData];
    
    [self setUI];
    
    
}


-(void)setUI{
    
    self.addTitleAddressView = [[ZHFAddTitleAddressView alloc]init];
    self.addTitleAddressView.title = @"选择地址";
    self.addTitleAddressView.userID = 7;
    self.addTitleAddressView.delegate1 = self;
    self.addTitleAddressView.defaultHeight = 350;
    self.addTitleAddressView.titleScrollViewH = 37;
    [self.view addSubview:[self.addTitleAddressView initAddressView]];
    
}

- (void)setData{
    
    AddressModel *model = [AddressList sharedInstance].selectAddressModel;
    
    peopleTf.text = model.name;
    phoneTf.text = model.phone;
    NSArray *array = [model.address componentsSeparatedByString:@"-"];
    NSMutableString *address = [NSMutableString string];
    NSString *detailAdress = array[array.count - 1];
    if (array.count == 1) {
        
        [address appendFormat:@"%@",array[0]];
    }else{
        for (int i = 0; i< array.count - 1; i++) {
            
            if (i == 0) {
                
                [address appendFormat:@"%@",array[i]];
            }else{
                
                [address appendFormat:@" %@",array[i]];
            }
            
        }
    }
    
    
    localTf.text = address;
    detalLocalTf.text = detailAdress;
    
    parametersModel = [[AddAddressParametersModel alloc] init];
    
    parametersModel.uid = model.uid;
    parametersModel.name = self->peopleTf.text;
    parametersModel.phone = self->phoneTf.text;
    parametersModel.address = self->detalLocalTf.text;
    NSMutableString *region = [NSMutableString string];
    if (model.state.length > 0) {
        
        [region appendFormat:@"%@",model.state];
    }
    
    if (model.city.length > 0) {
        
        [region appendFormat:@" %@",model.city];
    }
    
    if (model.district.length >0) {
        
        [region appendFormat:@" %@",model.district];
    }
    
    parametersModel.region = region;
}

- (IBAction)onSelectAdressBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    [self.addTitleAddressView addAnimate];
}

-(void)cancelBtnClick:(NSString *)titleAddress titleID:(NSString *)titleID{
    
    if(titleAddress.length <= 0)
    {
        return;
    }
    
    localTf.text = titleAddress;
    parametersModel.region = titleID;
    NSLog( @"%@", [NSString stringWithFormat:@"打印的对应省市县的id=%@",titleID]);
}

- (IBAction)onPostBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if (peopleTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入收货人"];
    }
    
    if (phoneTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入联系电话"];
    }
    
    if (![phoneTf.text isVaildMobileNo]) {
        
        return [self showMBPError:@"请输入正确手机号"];
    }
    
    if (localTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入所在区域"];
    }
    
    if (detalLocalTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入详细地址"];
    }
    
    parametersModel.name = peopleTf.text;
    parametersModel.phone = phoneTf.text;
    parametersModel.address = detalLocalTf.text;
    
    AddAddressStore *store = [[AddAddressStore alloc] init];
    
    MJWeakSelf
    [store editAdresstWithParameters:parametersModel WithSuccess:^{
        
        [weakSelf showMBPError:@"修改成功"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"AddAdressSuccess" object:nil];
        //GCD延迟
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        });
        
        
    } failure:^(NSError * error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
